
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;

import javax.swing.filechooser.FileFilter;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author yjpark
 */
public class AndroidReverseJFrame extends javax.swing.JFrame {

    final private String FS = System.getProperty("file.separator");

    final private String CLASSES_DEX_STRING = "classes.dex";
    final private String CLASSES_JAR_STRING = "classes-dex2jar.jar";
    final private String SRC = "src";
    final private String BIN = "bin";

    final private String OUTPUT_DIR_SUBS = "%OUTPUT_DIR%";
    final private String APK_FILE_NAME_SUBS = "%APK_FILE_NAME%";
    final private String APK_FILE_SRC_SUBS = "%APK_FILE_SRC%";
    final private String APK_FILE_DIR_SUBS = "%APK_FILE_DIR%";
    final private String ALL_FILES_SUBS = "%ALL_FILES%";

    private File recentWorkingDirectory = new java.io.File(".");
    
    private boolean renewal = false;

    private String outputDirectoryTextFieldParameter = "";
    private String apkFileSourceTextFieldParameter = "";
    private String apktoolParameterTextFieldParameter = "";
    private String dex2jarParameterTextFieldParameter = "";
    private String jadParameterTextFieldParameter = "";
    private String jdguiParameterTextFieldParameter = "";
    private String cleanDummyFilesParameterTextFieldParameter = "";
    private String eclipseProjectParameterTextFieldParameter = "";

    private String outputDirectory = "";
    private String apkFileName = "";
    private String apkFileDirectory = "";

    /**
     * Creates new form AndroidReverseJFrame
     */
    public AndroidReverseJFrame() {

        PrintStream origOut = System.out;
        PrintStream interceptor = new Interceptor(origOut);
        System.setOut(interceptor);
        System.setErr(interceptor);

        initComponents();
        initParameters();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        fileChooser = new javax.swing.JFileChooser();
        apkSrcButtonGroup = new javax.swing.ButtonGroup();
        apkFileSourcePanel = new javax.swing.JPanel();
        fromADBRadioButton = new javax.swing.JRadioButton();
        fromLocalDirectoryRadioButton = new javax.swing.JRadioButton();
        apkFileSourceTextField = new javax.swing.JTextField();
        apkFileSourceSelectButton = new javax.swing.JButton();
        consoleLogPanel = new javax.swing.JPanel();
        consoleLogScrollPane = new javax.swing.JScrollPane();
        consoleLogTextArea = new javax.swing.JTextArea();
        apktoolPanel = new javax.swing.JPanel();
        apktoolParameterLabel = new javax.swing.JLabel();
        apktoolRunButton = new javax.swing.JButton();
        apktoolParameterTextField = new javax.swing.JTextField();
        dex2jarPanel = new javax.swing.JPanel();
        dex2jarParameterLabel = new javax.swing.JLabel();
        dex2jarRunButton = new javax.swing.JButton();
        dex2jarParameterTextField = new javax.swing.JTextField();
        jadPanel = new javax.swing.JPanel();
        jadParameterLabel = new javax.swing.JLabel();
        jadRunButton = new javax.swing.JButton();
        jadParameterTextField = new javax.swing.JTextField();
        jdguiPanel = new javax.swing.JPanel();
        jdguiParameterLabel = new javax.swing.JLabel();
        jdguiRunButton = new javax.swing.JButton();
        jdguiParameterTextField = new javax.swing.JTextField();
        cleanDummyFilesPanel = new javax.swing.JPanel();
        cleanDummyFilesParameterLabel = new javax.swing.JLabel();
        cleanDummyFilesRunButton = new javax.swing.JButton();
        cleanDummyFilesParameterTextField = new javax.swing.JTextField();
        eclipseProjectPanel = new javax.swing.JPanel();
        eclipseProjectParameterLabel = new javax.swing.JLabel();
        eclipseProjectRunButton = new javax.swing.JButton();
        eclipseProjectParameterTextField = new javax.swing.JTextField();
        outputDirectoryPanel = new javax.swing.JPanel();
        outputDirectoryTextField = new javax.swing.JTextField();
        outputDirectorySelectButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Andoid Reverse Tools");
        setName("androidReverseFrame"); // NOI18N
        setPreferredSize(new java.awt.Dimension(500, 670));

        apkFileSourcePanel.setBorder(javax.swing.BorderFactory.createTitledBorder("APK File Source"));

        apkSrcButtonGroup.add(fromADBRadioButton);
        fromADBRadioButton.setText("From ADB");
        fromADBRadioButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fromADBRadioButtonActionPerformed(evt);
            }
        });

        apkSrcButtonGroup.add(fromLocalDirectoryRadioButton);
        fromLocalDirectoryRadioButton.setSelected(true);
        fromLocalDirectoryRadioButton.setText("From Local Directory");

        apkFileSourceTextField.setBackground(new java.awt.Color(255, 255, 255));

        apkFileSourceSelectButton.setText("Select");
        apkFileSourceSelectButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                apkFileSourceSelectButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout apkFileSourcePanelLayout = new javax.swing.GroupLayout(apkFileSourcePanel);
        apkFileSourcePanel.setLayout(apkFileSourcePanelLayout);
        apkFileSourcePanelLayout.setHorizontalGroup(
            apkFileSourcePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(apkFileSourcePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(apkFileSourcePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(apkFileSourcePanelLayout.createSequentialGroup()
                        .addComponent(fromLocalDirectoryRadioButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(fromADBRadioButton)
                        .addGap(0, 214, Short.MAX_VALUE))
                    .addGroup(apkFileSourcePanelLayout.createSequentialGroup()
                        .addComponent(apkFileSourceTextField)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(apkFileSourceSelectButton)))
                .addContainerGap())
        );
        apkFileSourcePanelLayout.setVerticalGroup(
            apkFileSourcePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(apkFileSourcePanelLayout.createSequentialGroup()
                .addGroup(apkFileSourcePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(fromADBRadioButton)
                    .addComponent(fromLocalDirectoryRadioButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(apkFileSourcePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(apkFileSourceTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(apkFileSourceSelectButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        consoleLogPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Console Log"));

        consoleLogTextArea.setEditable(false);
        consoleLogTextArea.setColumns(20);
        consoleLogTextArea.setLineWrap(true);
        consoleLogTextArea.setRows(5);
        consoleLogScrollPane.setViewportView(consoleLogTextArea);

        javax.swing.GroupLayout consoleLogPanelLayout = new javax.swing.GroupLayout(consoleLogPanel);
        consoleLogPanel.setLayout(consoleLogPanelLayout);
        consoleLogPanelLayout.setHorizontalGroup(
            consoleLogPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(consoleLogScrollPane)
        );
        consoleLogPanelLayout.setVerticalGroup(
            consoleLogPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(consoleLogScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 173, Short.MAX_VALUE)
        );

        apktoolPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("apktool"));

        apktoolParameterLabel.setText("parameters");

        apktoolRunButton.setText("Run");
        apktoolRunButton.setMaximumSize(new java.awt.Dimension(67, 23));
        apktoolRunButton.setMinimumSize(new java.awt.Dimension(67, 23));
        apktoolRunButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                apktoolRunButtonActionPerformed(evt);
            }
        });

        apktoolParameterTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                apktoolParameterTextFieldActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout apktoolPanelLayout = new javax.swing.GroupLayout(apktoolPanel);
        apktoolPanel.setLayout(apktoolPanelLayout);
        apktoolPanelLayout.setHorizontalGroup(
            apktoolPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(apktoolPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(apktoolParameterLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(apktoolParameterTextField)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(apktoolRunButton, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        apktoolPanelLayout.setVerticalGroup(
            apktoolPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(apktoolPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(apktoolParameterLabel)
                .addComponent(apktoolRunButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(apktoolParameterTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        dex2jarPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("dex2jar"));

        dex2jarParameterLabel.setText("parameters");

        dex2jarRunButton.setText("Run");
        dex2jarRunButton.setMaximumSize(new java.awt.Dimension(67, 23));
        dex2jarRunButton.setMinimumSize(new java.awt.Dimension(67, 23));
        dex2jarRunButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                dex2jarRunButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout dex2jarPanelLayout = new javax.swing.GroupLayout(dex2jarPanel);
        dex2jarPanel.setLayout(dex2jarPanelLayout);
        dex2jarPanelLayout.setHorizontalGroup(
            dex2jarPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(dex2jarPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(dex2jarParameterLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(dex2jarParameterTextField)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(dex2jarRunButton, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        dex2jarPanelLayout.setVerticalGroup(
            dex2jarPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(dex2jarPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(dex2jarParameterLabel)
                .addComponent(dex2jarRunButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(dex2jarParameterTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        jadPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("jad"));

        jadParameterLabel.setText("parameters");

        jadRunButton.setText("Run");
        jadRunButton.setMaximumSize(new java.awt.Dimension(67, 23));
        jadRunButton.setMinimumSize(new java.awt.Dimension(67, 23));
        jadRunButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jadRunButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jadPanelLayout = new javax.swing.GroupLayout(jadPanel);
        jadPanel.setLayout(jadPanelLayout);
        jadPanelLayout.setHorizontalGroup(
            jadPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jadPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jadParameterLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jadParameterTextField)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jadRunButton, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jadPanelLayout.setVerticalGroup(
            jadPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jadPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jadParameterLabel)
                .addComponent(jadRunButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jadParameterTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        jdguiPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("jd-gui"));

        jdguiParameterLabel.setText("parameters");

        jdguiRunButton.setText("Run");
        jdguiRunButton.setMaximumSize(new java.awt.Dimension(67, 23));
        jdguiRunButton.setMinimumSize(new java.awt.Dimension(67, 23));
        jdguiRunButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jdguiRunButtonActionPerformed(evt);
            }
        });

        jdguiParameterTextField.setEditable(false);

        javax.swing.GroupLayout jdguiPanelLayout = new javax.swing.GroupLayout(jdguiPanel);
        jdguiPanel.setLayout(jdguiPanelLayout);
        jdguiPanelLayout.setHorizontalGroup(
            jdguiPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jdguiPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jdguiParameterLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jdguiParameterTextField)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jdguiRunButton, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jdguiPanelLayout.setVerticalGroup(
            jdguiPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jdguiPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jdguiParameterLabel)
                .addComponent(jdguiRunButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jdguiParameterTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        cleanDummyFilesPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Clean Dummy Files"));

        cleanDummyFilesParameterLabel.setText("parameters");

        cleanDummyFilesRunButton.setText("Run");
        cleanDummyFilesRunButton.setMaximumSize(new java.awt.Dimension(67, 23));
        cleanDummyFilesRunButton.setMinimumSize(new java.awt.Dimension(67, 23));
        cleanDummyFilesRunButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cleanDummyFilesRunButtonActionPerformed(evt);
            }
        });

        cleanDummyFilesParameterTextField.setEditable(false);

        javax.swing.GroupLayout cleanDummyFilesPanelLayout = new javax.swing.GroupLayout(cleanDummyFilesPanel);
        cleanDummyFilesPanel.setLayout(cleanDummyFilesPanelLayout);
        cleanDummyFilesPanelLayout.setHorizontalGroup(
            cleanDummyFilesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(cleanDummyFilesPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(cleanDummyFilesParameterLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cleanDummyFilesParameterTextField)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cleanDummyFilesRunButton, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        cleanDummyFilesPanelLayout.setVerticalGroup(
            cleanDummyFilesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(cleanDummyFilesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(cleanDummyFilesParameterLabel)
                .addComponent(cleanDummyFilesRunButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(cleanDummyFilesParameterTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        eclipseProjectPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Eclipse Project"));

        eclipseProjectParameterLabel.setText("parameters");

        eclipseProjectRunButton.setText("Run");
        eclipseProjectRunButton.setMaximumSize(new java.awt.Dimension(67, 23));
        eclipseProjectRunButton.setMinimumSize(new java.awt.Dimension(67, 23));
        eclipseProjectRunButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                eclipseProjectRunButtonActionPerformed(evt);
            }
        });

        eclipseProjectParameterTextField.setEditable(false);

        javax.swing.GroupLayout eclipseProjectPanelLayout = new javax.swing.GroupLayout(eclipseProjectPanel);
        eclipseProjectPanel.setLayout(eclipseProjectPanelLayout);
        eclipseProjectPanelLayout.setHorizontalGroup(
            eclipseProjectPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(eclipseProjectPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(eclipseProjectParameterLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(eclipseProjectParameterTextField)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(eclipseProjectRunButton, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        eclipseProjectPanelLayout.setVerticalGroup(
            eclipseProjectPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(eclipseProjectPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(eclipseProjectParameterLabel)
                .addComponent(eclipseProjectRunButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(eclipseProjectParameterTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        outputDirectoryPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Output Directory"));

        outputDirectorySelectButton.setText("Select");
        outputDirectorySelectButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                outputDirectorySelectButtonMouseClicked(evt);
            }
        });
        outputDirectorySelectButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                outputDirectorySelectButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout outputDirectoryPanelLayout = new javax.swing.GroupLayout(outputDirectoryPanel);
        outputDirectoryPanel.setLayout(outputDirectoryPanelLayout);
        outputDirectoryPanelLayout.setHorizontalGroup(
            outputDirectoryPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(outputDirectoryPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(outputDirectoryTextField)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(outputDirectorySelectButton)
                .addContainerGap())
        );
        outputDirectoryPanelLayout.setVerticalGroup(
            outputDirectoryPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(outputDirectoryPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(outputDirectoryTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(outputDirectorySelectButton))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(apkFileSourcePanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(apktoolPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(dex2jarPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jadPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jdguiPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(cleanDummyFilesPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(eclipseProjectPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(consoleLogPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(outputDirectoryPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(outputDirectoryPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(apkFileSourcePanel, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(apktoolPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(dex2jarPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jadPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jdguiPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cleanDummyFilesPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(eclipseProjectPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(consoleLogPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        getAccessibleContext().setAccessibleName("Android Reverse Tools");

        pack();
    }// </editor-fold>                        

    private void apkFileSourceSelectButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                          
        // TODO add your handling code here:
        FileFilter filter = new ExtensionFileFilter("APK Files", new String[]{"APK"});

        fileChooser.setCurrentDirectory(recentWorkingDirectory);
        fileChooser.setDialogTitle("Select..");
        fileChooser.setFileSelectionMode(javax.swing.JFileChooser.OPEN_DIALOG);
        fileChooser.setFileFilter(filter);

        //
        // disable the "All files" option.
        //
        fileChooser.setAcceptAllFileFilterUsed(true);

        //    
        if (fileChooser.showOpenDialog(this) == javax.swing.JFileChooser.APPROVE_OPTION) {
            System.out.println("getCurrentDirectory(): "
                    + fileChooser.getCurrentDirectory());
            System.out.println("getSelectedFile() : "
                    + fileChooser.getSelectedFile());

            recentWorkingDirectory = fileChooser.getSelectedFile();
            
            apkFileSourceTextFieldParameter = "\"" + fileChooser.getSelectedFile().toString() + "\"";
            apkFileSourceTextField.setText(apkFileSourceTextFieldParameter);

            String[] tempArray =  fileChooser.getSelectedFile().toString().split("\\" + FS);
            apkFileName = tempArray[tempArray.length - 1];

            tempArray = apkFileName.split("\\.");
            apkFileDirectory = "\"" + outputDirectory.replaceAll("\"", "") + FS + tempArray[0] + "\"";
            
            renewal = true;
            substituteParameter(APK_FILE_SRC_SUBS, apkFileSourceTextFieldParameter);
            substituteParameter(APK_FILE_DIR_SUBS, apkFileDirectory);
        } else {
            System.out.println("No Selection ");
        }
    }                                                         

    private void outputDirectorySelectButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                            
        // TODO add your handling code here:
        fileChooser.setCurrentDirectory(recentWorkingDirectory);
        fileChooser.setDialogTitle("Select..");
        fileChooser.setFileSelectionMode(javax.swing.JFileChooser.DIRECTORIES_ONLY);

        //
        // disable the "All files" option.
        //
        fileChooser.setAcceptAllFileFilterUsed(false);

        //    
        if (fileChooser.showOpenDialog(this) == javax.swing.JFileChooser.APPROVE_OPTION) {
            System.out.println("getCurrentDirectory(): "
                    + fileChooser.getCurrentDirectory());
            System.out.println("getSelectedFile() : "
                    + fileChooser.getSelectedFile());

            recentWorkingDirectory = fileChooser.getSelectedFile();

            outputDirectoryTextFieldParameter = "\"" + fileChooser.getSelectedFile().toString() + "\"";
            outputDirectoryTextField.setText(outputDirectoryTextFieldParameter);

            outputDirectory = outputDirectoryTextFieldParameter;
            
            renewal = true;
            substituteParameter(OUTPUT_DIR_SUBS, outputDirectory);            
        } else {
            System.out.println("No Selection ");
        }
    }                                                           

    private void fromADBRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                   
        // TODO add your handling code here:
    }                                                  

    private void outputDirectorySelectButtonMouseClicked(java.awt.event.MouseEvent evt) {                                                         
        // TODO add your handling code here:
    }                                                        

    private void apktoolRunButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                 
        // TODO add your handling code here:
//    	ExecuteUtil.callExecute("cmd /c start apktool.bat", apktoolParameterTextField.getText()); // not work, parameter passing error
    	try {
			Runtime.getRuntime().exec("cmd /c start apktool.bat " + apktoolParameterTextField.getText());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }                                                

    private void dex2jarRunButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                 
        // TODO add your handling code here:
    	// move APK_FILE_DIR and unzip apk classes.dex
    	try {
			Runtime.getRuntime().exec("cmd /c start get-classes_dex.bat " + apkFileDirectory + " " + apkFileName);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	
    	// dex2jar
    	try {
			Runtime.getRuntime().exec("cmd /c start d2j-dex2jar.bat " + dex2jarParameterTextField.getText());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }                                                

    private void jadRunButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             
        // TODO add your handling code here:
    }                                            

    private void jdguiRunButtonActionPerformed(java.awt.event.ActionEvent evt) {                                               
        // TODO add your handling code here:
    	new Thread(startJDGUI).run(); // blocking mode - have to fix
    }                                              

    private void cleanDummyFilesRunButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                         
        // TODO add your handling code here:
    }                                                        

    private void eclipseProjectRunButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                        
        // TODO add your handling code here:
    }                                                       

    private void apktoolParameterTextFieldActionPerformed(java.awt.event.ActionEvent evt) {                                                          
        // TODO add your handling code here:
    }                                                         

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Metal".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(AndroidReverseJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new AndroidReverseJFrame().setVisible(true);
            }
        });
    }

    private void initParameters() {

        outputDirectoryTextFieldParameter = OUTPUT_DIR_SUBS;
        apkFileSourceTextFieldParameter = APK_FILE_SRC_SUBS;
        apktoolParameterTextFieldParameter = "d " + APK_FILE_SRC_SUBS + " " + APK_FILE_DIR_SUBS;
        dex2jarParameterTextFieldParameter = "-v -o " + APK_FILE_DIR_SUBS + FS + CLASSES_JAR_STRING + " " + APK_FILE_DIR_SUBS + FS + CLASSES_DEX_STRING;
        jadParameterTextFieldParameter = "-r -v -d " + APK_FILE_DIR_SUBS + FS + SRC + " -s java " + ALL_FILES_SUBS;
        jdguiParameterTextFieldParameter = APK_FILE_DIR_SUBS + FS + CLASSES_JAR_STRING;
        cleanDummyFilesParameterTextFieldParameter = "";
        eclipseProjectParameterTextFieldParameter = "";

        outputDirectoryTextField.setText(outputDirectoryTextFieldParameter);
        apkFileSourceTextField.setText(apkFileSourceTextFieldParameter);
        apktoolParameterTextField.setText(apktoolParameterTextFieldParameter);
        dex2jarParameterTextField.setText(dex2jarParameterTextFieldParameter);
        jadParameterTextField.setText(jadParameterTextFieldParameter);
        jdguiParameterTextField.setText(jdguiParameterTextFieldParameter);
        cleanDummyFilesParameterTextField.setText(cleanDummyFilesParameterTextFieldParameter);
        eclipseProjectParameterTextField.setText(eclipseProjectParameterTextFieldParameter);
    }

    private void substituteParameter(String targetSubs, String targetParameter) {
        // path character
        if (FS.equals("\\")) // Windows case
        {
            targetParameter = targetParameter.replaceAll("\\\\", "\\\\\\\\");
        }
        
        if(renewal) {
	        apktoolParameterTextField.setText(apktoolParameterTextFieldParameter.replaceAll(targetSubs, targetParameter));
	        dex2jarParameterTextField.setText(dex2jarParameterTextFieldParameter.replaceAll(targetSubs, targetParameter));
	        jadParameterTextField.setText(jadParameterTextFieldParameter.replaceAll(targetSubs, targetParameter));
	        jdguiParameterTextField.setText(jdguiParameterTextFieldParameter.replaceAll(targetSubs, targetParameter));
	        cleanDummyFilesParameterTextField.setText(cleanDummyFilesParameterTextFieldParameter.replaceAll(targetSubs, targetParameter));
	        eclipseProjectParameterTextField.setText(eclipseProjectParameterTextFieldParameter.replaceAll(targetSubs, targetParameter));
	        
	        renewal = !renewal;
        } else {
        	apktoolParameterTextField.setText(apktoolParameterTextField.getText().replaceAll(targetSubs, targetParameter));
	        dex2jarParameterTextField.setText(dex2jarParameterTextField.getText().replaceAll(targetSubs, targetParameter));
	        jadParameterTextField.setText(jadParameterTextField.getText().replaceAll(targetSubs, targetParameter));
	        jdguiParameterTextField.setText(jdguiParameterTextField.getText().replaceAll(targetSubs, targetParameter));
	        cleanDummyFilesParameterTextField.setText(cleanDummyFilesParameterTextField.getText().replaceAll(targetSubs, targetParameter));
	        eclipseProjectParameterTextField.setText(eclipseProjectParameterTextField.getText().replaceAll(targetSubs, targetParameter));
        }
    }

    private class ExtensionFileFilter extends FileFilter {

        String description;

        String extensions[];

        public ExtensionFileFilter(String description, String extensions[]) {
            if (description == null) {
                this.description = extensions[0];
            } else {
                this.description = description;
            }
            this.extensions = (String[]) extensions.clone();
            toLower(this.extensions);
        }

        private void toLower(String array[]) {
            for (int i = 0, n = array.length; i < n; i++) {
                array[i] = array[i].toLowerCase();
            }
        }

        @Override
        public String getDescription() {
            return description;
        }

        @Override
        public boolean accept(File file) {
            if (file.isDirectory()) {
                return true;
            } else {
                String path = file.getAbsolutePath().toLowerCase();
                for (int i = 0, n = extensions.length; i < n; i++) {
                    String extension = extensions[i];
                    if ((path.endsWith(extension) && (path.charAt(path.length() - extension.length() - 1)) == '.')) {
                        return true;
                    }
                }
            }
            return false;
        }
    }

    private class Interceptor extends PrintStream {

        public Interceptor(OutputStream out) {
            super(out, true);
        }

        @Override
        public void print(String s) {
            super.print(s);

            String oldLog = consoleLogTextArea.getText();

            consoleLogTextArea.setText(oldLog + "\n" + s);
            consoleLogTextArea.setCaretPosition(consoleLogTextArea.getDocument().getLength());
        }
    }

	private Runnable startJDGUI = new Runnable() {
		@Override
		public void run() {
			ExecuteUtil.callExecute("jd-gui.exe", jdguiParameterTextField.getText());
		}
	};
	
    // Variables declaration - do not modify                     
    private javax.swing.JPanel apkFileSourcePanel;
    private javax.swing.JButton apkFileSourceSelectButton;
    private javax.swing.JTextField apkFileSourceTextField;
    private javax.swing.ButtonGroup apkSrcButtonGroup;
    private javax.swing.JPanel apktoolPanel;
    private javax.swing.JLabel apktoolParameterLabel;
    private javax.swing.JTextField apktoolParameterTextField;
    private javax.swing.JButton apktoolRunButton;
    private javax.swing.JPanel cleanDummyFilesPanel;
    private javax.swing.JLabel cleanDummyFilesParameterLabel;
    private javax.swing.JTextField cleanDummyFilesParameterTextField;
    private javax.swing.JButton cleanDummyFilesRunButton;
    private javax.swing.JPanel consoleLogPanel;
    private javax.swing.JScrollPane consoleLogScrollPane;
    private javax.swing.JTextArea consoleLogTextArea;
    private javax.swing.JPanel dex2jarPanel;
    private javax.swing.JLabel dex2jarParameterLabel;
    private javax.swing.JTextField dex2jarParameterTextField;
    private javax.swing.JButton dex2jarRunButton;
    private javax.swing.JPanel eclipseProjectPanel;
    private javax.swing.JLabel eclipseProjectParameterLabel;
    private javax.swing.JTextField eclipseProjectParameterTextField;
    private javax.swing.JButton eclipseProjectRunButton;
    private javax.swing.JFileChooser fileChooser;
    private javax.swing.JRadioButton fromADBRadioButton;
    private javax.swing.JRadioButton fromLocalDirectoryRadioButton;
    private javax.swing.JPanel jadPanel;
    private javax.swing.JLabel jadParameterLabel;
    private javax.swing.JTextField jadParameterTextField;
    private javax.swing.JButton jadRunButton;
    private javax.swing.JPanel jdguiPanel;
    private javax.swing.JLabel jdguiParameterLabel;
    private javax.swing.JTextField jdguiParameterTextField;
    private javax.swing.JButton jdguiRunButton;
    private javax.swing.JPanel outputDirectoryPanel;
    private javax.swing.JButton outputDirectorySelectButton;
    private javax.swing.JTextField outputDirectoryTextField;
    // End of variables declaration                   
}
